package su.myspringapps.online_school_learning_spring_data_jpa.repository;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import su.myspringapps.online_school_learning_spring_data_jpa.entity.Student;
import su.myspringapps.online_school_learning_spring_data_jpa.repository.main_db.StudentRepository;

import java.util.Date;
import java.util.List;

@SpringBootTest
class StudentRepositoryTest {

    @Autowired
    StudentRepository studentRepository;

    @Test
    void findFirstAndTop() {
        List<Student> first10Students = studentRepository.findFirst10ByOrderByAgeDesc();
        List<Student> top10Students = studentRepository.findTop10ByOrderByAgeDesc();
        int id = 26;
        int age = 43;
        String name = "Шведов Юрий";
        Student studentOne = first10Students.get(0);
        Student studentTwo = top10Students.get(0);
        Assertions.assertEquals(id, studentOne.getId());
        Assertions.assertEquals(age, studentOne.getAge());
        Assertions.assertEquals(name, studentOne.getName());
        Assertions.assertEquals(id, studentTwo.getId());
        Assertions.assertEquals(age, studentTwo.getAge());
        Assertions.assertEquals(name, studentTwo.getName());
    }

    @Test
    void findById() {
        Student actualStudent = studentRepository.findById(1);
        Assertions.assertEquals(1, actualStudent.getId());
        Assertions.assertEquals("Фуриков Эрнст", actualStudent.getName());
        Assertions.assertEquals(18, actualStudent.getAge());
    }

    @Test
    void findStudentById() {
        Student actualStudent = studentRepository.findStudentById(1);
        Assertions.assertNotNull(actualStudent);
        Assertions.assertEquals(1, actualStudent.getId());
        Assertions.assertEquals("Фуриков Эрнст", actualStudent.getName());
        Assertions.assertEquals(18, actualStudent.getAge());
    }

    @Test
    void findAll() {
        List<Student> studentList = studentRepository.findAll();
        Student actualStudent1 = studentList.get(0);
        Student actualStudent2 = studentList.get(1);
        Student actualStudent3 = studentList.get(2);
        Student actualStudent4 = studentList.get(3);

        Assertions.assertEquals(2, actualStudent1.getId());
        Assertions.assertEquals("Амбражевич Порфирий", actualStudent1.getName());
        Assertions.assertEquals(19, actualStudent1.getAge());

        Assertions.assertEquals(3, actualStudent2.getId());
        Assertions.assertEquals("Носов Макар", actualStudent2.getName());
        Assertions.assertEquals(20, actualStudent2.getAge());

        Assertions.assertEquals(4, actualStudent3.getId());
        Assertions.assertEquals("Кооскора Вениамин", actualStudent3.getName());
        Assertions.assertEquals(21, actualStudent3.getAge());

        Assertions.assertEquals(5, actualStudent4.getId());
        Assertions.assertEquals("Беленков Денис", actualStudent4.getName());
        Assertions.assertEquals(22, actualStudent4.getAge());

    }

    @Test
    void save() {
        Student expectedStudent = new Student();
        expectedStudent.setName("Навотно Стоечко");
        expectedStudent.setAge(18);
        expectedStudent.setRegistrationDate(new Date());
        Integer id = studentRepository.save(expectedStudent).getId();
        Student actualStudent = studentRepository.findById(id);
        Assertions.assertEquals(expectedStudent, actualStudent);
        studentRepository.delete(expectedStudent);
        actualStudent = studentRepository.findById(id);
        Assertions.assertNull(actualStudent);
    }

    @Test
    void deleteById() {
        Student studentForDelete = new Student();
        studentForDelete.setAge(19);
        studentForDelete.setName("Седайко Стюмчик");
        studentForDelete.setRegistrationDate(new Date());
        Integer id = studentRepository.save(studentForDelete).getId();
        Student actualStudent = studentRepository.findById(id);
        Assertions.assertEquals(studentForDelete, actualStudent);
        studentRepository.deleteById(id);
        actualStudent = studentRepository.findById(id);
        Assertions.assertNull(actualStudent);
    }

    @Test
    void delete() {
        Student studentForDelete = new Student();
        studentForDelete.setAge(19);
        studentForDelete.setName("Седайко Стюмчик");
        studentForDelete.setRegistrationDate(new Date());
        Integer id = studentRepository.save(studentForDelete).getId();
        Student actualStudent = studentRepository.findById(id);
        Assertions.assertEquals(studentForDelete, actualStudent);
        studentRepository.delete(studentForDelete);
        actualStudent = studentRepository.findById(id);
        Assertions.assertNull(actualStudent);
    }
}